package cn.edu.njnu.controller;

import cn.edu.njnu.dto.ChangePasswordDto;
import cn.edu.njnu.dto.UserRegisterDto;
import cn.edu.njnu.pojo.Result;
import cn.edu.njnu.pojo.ResultFactory;
import cn.edu.njnu.pojo.User;
import cn.edu.njnu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.validation.Valid;

@RestController
@RequestMapping("/e-resource/api")
public class RegisterController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/v1.0/public/register")
    public Result register(@RequestBody @Validated UserRegisterDto user) {
        String username = user.getUsername();
        username = HtmlUtils.htmlEscape(username);
        user.setUsername(username);
        if (userService.isExist(user.getUsername(),user.getEmail())){
            return ResultFactory.buildFailResult("用户名或邮箱已被占用");
        }
        return userService.register(user);
    }
    @GetMapping("/v1.0/public/school/{area_code}")
    public Result getSchools(@PathVariable("area_code") Integer code){
        return userService.getSchools(code);
    }
    @PostMapping("/v1.0/private/changePwd")
    public Result changePassword(@RequestBody ChangePasswordDto changePasswordDto){
        return userService.changePassword(changePasswordDto);
    }
}
